﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataCentral.UsecaseControllers;
using DataCentral.BusinessDataObjects;
using CommonData;

namespace DataCentral.Forms
{
    public partial class ProfileForm : CommonChildForm
    {
        private ProfileUsecase _UCProfile;
        private UserBO _UserBO;
        private bool _IsPasswordChanged = false;

        private bool IsValidateUserInfo()
        {
            bool isValid = true;
            errProvider.ClearErrors();
            if (cboRole.EditValue == null)
            {
                CommonFunctions.ShowErrorProvider(errProvider, cboRole, ConstantClass._Error_RoleNotBlank);
                isValid = false;
            }

            if (_IsPasswordChanged)
            {
                if (string.IsNullOrEmpty(txtConfirmPass.Text))
                {
                    CommonFunctions.ShowErrorProvider(errProvider, txtConfirmPass, ConstantClass._Error_VerifyPassNotBlank);
                    isValid = false;
                }
                else if (!txtConfirmPass.Text.Equals(txtPassword.Text))
                {
                    CommonFunctions.ShowErrorProvider(errProvider, txtPassword, ConstantClass._Error_InvalidVerifyPass);
                    isValid = false;
                }
            }

            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                CommonFunctions.ShowErrorProvider(errProvider, txtPassword, ConstantClass._Error_PassNotBlank);
                isValid = false;
            }

            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                CommonFunctions.ShowErrorProvider(errProvider, txtUserName, ConstantClass._Error_UserNotBlank);
                isValid = false;
            }
            return isValid;
        }

        public ProfileForm(ProfileUsecase _UCProfile, UserBO _UserBO)
        {
            InitializeComponent();
            this._UCProfile = _UCProfile;
            this._UserBO = _UserBO;
            btnSave.ToolTip = "Lưu thông tin của tài khoản.\r\n\r\nPhím nóng: Ctrl + S";
            btnClose.ToolTip = "Đóng màn hình thông tin của tài khoản.\r\n\r\nPhím nóng: Escape";
        }

        private void ProfileForm_Load(object sender, EventArgs e)
        {
            ValueListClass clValueList = new ValueListClass();
            cboRole.Properties.DataSource = clValueList.GetRoleList();
            cboCreatedUser.Properties.DataSource = clValueList.GetUserList();
            cboLastUpdatedUser.Properties.DataSource = cboCreatedUser.Properties.DataSource;
            bsUserBO.DataSource = _UserBO;
            _IsPasswordChanged = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (IsValidateUserInfo())
            {
                bsUserBO.EndEdit();
                _UserBO.LastUpdatedDate = DateTime.Now;
                _UserBO.LastUpdatedUserID = StaticClass.GetUserLogin();
                UserBO userBO = new UserBO();
                userBO.UpdateUserBO(_UserBO);
                this.Close();
            }
            else
            {
                MessageClass.ShowWarningMessage("Dữ liệu không hợp lệ. Hãy kiểm tra lại ở nhưng nơi được đánh dấu!");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProfileForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.S)
                btnSave_Click(null, null);
        }

        private void txtPassword_EditValueChanged(object sender, EventArgs e)
        {
            _IsPasswordChanged = true;
        }
    }
}